﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label ">优惠劵名称<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="title" col="title" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">面值<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="coupon_price" col="coupon_price" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">使用门槛<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="coupon_threshold_price" col="coupon_threshold_price" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">领取的日期<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="enable_time" col="enable_time" type="text" class="input-sm form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">有效时间<font class="red"> *</font></label>
            <div class="col-sm-8 input-daterange input-group">
                <input type="text" class="input-sm form-control" style="width: 94%;margin-left:6%" id="start_time" col="start_time" placeholder="yyyy-MM-dd" />
                <span class="input-group-addon" style="margin-left:-10%">至</span>
                <input type="text" class="input-sm form-control" style="width: 94%" id="end_time" col="end_time" placeholder="yyyy-MM-dd" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">发行数量<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="create_num" col="create_num" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">用户限领数量<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="limit_num" col="limit_num" value="1" type="text"  readonly class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">优惠卷类型<font class="red"> *</font></label>
            <div class="col-sm-8" id="type" col="type">

            </div>
        </div>
       
        <div class="form-group">
            <label class="col-sm-3 control-label ">可使用范围<font class="red"> *</font></label>
            <div class="col-sm-8" id="use_type" col="use_type">

            </div>
        </div>
        <div class="form-group" style="display: none">
            <label class="col-sm-3 control-label ">分类<font class="red"> *</font></label>
            <div class="col-sm-8" id="categoryids" col="categoryids">

            </div>
        </div>
        <div class="form-group" style="display: none ">
            <label class="col-sm-3 control-label ">商铺<font class="red"> *</font></label>
            <div class="col-sm-8" id="shopids" col="shopids">

            </div>
        </div>
        <div class="form-group" style="display: none ">
            <label class="col-sm-3 control-label ">商品<font class="red"> *</font></label>
            <div class="col-sm-8" id="goodsids" col="goodsids">

            </div>
        </div>
        <div class="form-group" style="display:none">
            <label class="col-sm-3 control-label ">备注</label>
            <div class="col-sm-8">
                <input id="note" col="note" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">使用范围说明<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="scope_desc" col="scope_desc" type="text" class="form-control" />
            </div>
        </div>
    </form>
    @*<form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">长输入框</label>
            <div class="layui-input-block">
                <input type="text" name="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
    </form>*@
</div>

<script type="text/javascript">
    var id = ys.request("id");

    $(function () {
        laydate.render({ elem: '#enable_time', format: 'yyyy-MM-dd' });
        $("#type").ysComboBox({
            url: '@Url.Content("~/QixingManage/Coupon/GetCouponTypeListJson")',
            class: "form-control",
        });
       
        $('#categoryids').ysComboBoxTree({ url: '@Url.Content("~/QixingManage/GoodsCategory/GetCategoryTreeListJson")' });

        $("#shopids").ysComboBox({
            url: '@Url.Content("~/QixingManage/Shop/GetListJson")',
            key: "Id",
            value: "shop_name",
            class: "form-control",
            multiple: true
        });

        $("#goodsids").ysComboBox({
            url: '@Url.Content("~/QixingManage/Goods/GetListJson")',
            key: "Id",
            value: "goods_name",
            class: "form-control",
            multiple: true
        });

        $("#use_type").ysComboBox({
            data: ys.getJson(@Html.Raw(typeof(use_type__enum).EnumToDictionaryString())),
            class: "form-control",
            onChange: function (val)
            {
                var seledval = parseInt($('#use_type').ysComboBox('getValue'));
                switch (seledval) {
                    case 0://全平台
                        $("#note").parent().parent().show();
                        $("#categoryids").parent().hide();
                        $("#shopids").parent().hide();
                        $("#goodsids").parent().hide();
                        break;
                    case 1://分类
                        $("#note").parent().parent().show();
                        $("#categoryids").parent().show();
                        $("#shopids").parent().hide();
                        $("#goodsids").parent().hide();
                        break;
                    case 2: //商店
                        $("#note").parent().parent().show();
                        $("#categoryids").parent().hide();
                        $("#shopids").parent().show();
                        $("#goodsids").parent().hide();
                        break;
                    case 3: //商品
                        $("#note").parent().parent().show();
                        $("#categoryids").parent().hide();
                        $("#shopids").parent().hide();
                        $("#goodsids").parent().show();
                        break;
                    default:
                        $("#note").parent().parent().hide();
                        $("#categoryids").parent().hide();
                        $("#shopids").parent().hide();
                        $("#goodsids").parent().hide();
                        break;
                }

            }
        });
        var startDate = laydate.render({
            elem: '#start_time',
            max: $('#end_time').val(),
            theme: 'molv',
            trigger: 'click',
            done: function (value, date) {
                // 结束时间大于开始时间
                if (value !== '') {
                    endDate.config.min.year = date.year;
                    endDate.config.min.month = date.month - 1;
                    endDate.config.min.date = date.date;
                } else {
                    endDate.config.min.year = '';
                    endDate.config.min.month = '';
                    endDate.config.min.date = '';
                }
            }
        });

        var endDate = laydate.render({
            elem: '#end_time',
            min: $('#start_time').val(),
            theme: 'molv',
            trigger: 'click',
            done: function (value, date) {
                // 开始时间小于结束时间
                if (value !== '') {
                    startDate.config.max.year = date.year;
                    startDate.config.max.month = date.month - 1;
                    startDate.config.max.date = date.date;
                } else {
                    startDate.config.max.year = '';
                    startDate.config.max.month = '';
                    startDate.config.max.date = '';
                }
            }
        });

        getForm();

        $('#form').validate({
            rules: {
                title: { required: true }
            }
        });
    });

    function getForm() {
        if (id > 0) {
            ys.ajax({
                url: '@Url.Content("~/QixingManage/Coupon/GetFormJson")' + '?id=' + id,
                type: 'get',
                success: function (obj) {
                    if (obj.Tag == 1) {
                        $('#form').setWebControls(obj.Data);
                        var use_type = parseInt(obj.Data.use_type);
                        switch (use_type) {
                            case 0://全平台
                                $("#note").parent().parent().show();
                                $("#categoryids").parent().hide();
                                $("#shopids").parent().hide();
                                $("#goodsids").parent().hide();
                                break;
                            case 1://分类
                                $("#note").parent().parent().show();
                                $("#categoryids").parent().show();
                                $("#shopids").parent().hide();
                                $("#goodsids").parent().hide();
                                break;
                            case 2: //商店
                                $("#note").parent().parent().show();
                                $("#categoryids").parent().hide();
                                $("#shopids").parent().show();
                                $("#goodsids").parent().hide();

                                break;
                            case 3: //商品
                                $("#note").parent().parent().show();
                                $("#categoryids").parent().hide();
                                $("#shopids").parent().hide();
                                $("#goodsids").parent().show();
                                break;
                            default:
                                $("#note").parent().parent().hide();
                                $("#categoryids").parent().hide();
                                $("#shopids").parent().hide();
                                $("#goodsids").parent().hide();
                                break;
                        }
                    }
                }
            });
        }
        else {
            var defaultData = {};
            defaultData.limit_num = 1;
            $('#form').setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id });
            postData.categoryid = ys.getLastValue(postData.categoryids);
            ys.ajax({
                url: '@Url.Content("~/QixingManage/Coupon/SaveFormJson")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }
</script>

